package br.com.naybee.framework.client.models;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import static br.com.naybee.framework.client.utils.ValidatorUtils.*;

@SuppressWarnings("serial")
public class ListModel extends ArrayList<Model> {

	public Model newModel() {
		Model result = Models.newModel();
		add(result);
		return result;
	}

	public ListModel copy(ListModel listModel) {
		this.addAll(listModel);
		return this;
	}

	@SuppressWarnings("unchecked")
	public ListModel copy(ArrayList<?> list) {
		for (Object object : list) {
			Model model = Models.newModel();
			model.putAll((Map<String, Object>) object);
			add(model);
		}
		return this;
	}

	public ListModel add(ListModel listModel, ModelFieldMap modelFieldMap) {
		for (Model model : listModel) {
			Set<String> keys = model.keySet();
			Model resultModel = Models.newModel();
			for (String key : keys) {
				String destinationKey = modelFieldMap.getDestinationKey(key);
				if (notEmpty(destinationKey)) {
					resultModel.copy(model, key, destinationKey);
				}
			}
			if (notEmpty(resultModel)) {
				add(resultModel);
			}
		}
		return this;
	}
}
